

#ifndef	__ZUTIL_STRING_HPP__
#define	__ZUTIL_STRING_HPP__


#include	<string>
#include	<vector>
#include	<algorithm>

namespace   zutil
{

    // Convert string to UPPERCASE
    inline  const std::string& upperstring(std::string &src)
    {
        std::transform(src.begin(),src.end(),src.begin(),::toupper);
        return src;
    }  /* upperstring() */

    // Convert string yo LOWERCASE
    inline  const std::string& lowerstring(std::string &src)
    {
        std::transform(src.begin(),src.end(),src.begin(),::tolower);
        return src;
    }  /* lowerstring() */

    // Split string
    inline  unsigned int split(const std::string &chars,
            const std::string &separator,
            std::vector<std::string> &savelist,
            unsigned int iLimit = 0)
    {
        if ( chars.length() <= 0 || chars.empty() )
        {
            return 0;
        }

        if ( separator.length() <= 0 || separator.empty() )
        {
            return 0;
        }

        // Save splited substring into vector->savelist
        std::string::size_type pos = 0;
        std::string::size_type newpos = 0;
        while ( newpos >= 0 )
        {
            newpos = chars.find(separator,pos);
            if ( newpos == std::string::npos )
            {
                std::string str = "";
                str = chars.substr(pos,std::string::npos);
                if ( str != "" )
                {
                    savelist.push_back(str);
                }
                break;
            }
            savelist.push_back(chars.substr(pos,newpos - pos));
            pos = newpos + separator.length();

            if ( (iLimit > 0) && (savelist.size() >= (iLimit - 1)) )
            {
                std::string str = chars.substr ( pos, std::string::npos );
                savelist.push_back ( str );

                break;
            }
        }

        return savelist.size();
    }  /* split() */

    inline   const std::string&    trim ( std::string& str )
    {
        std::string::size_type first, last;

        first = str.find_first_not_of ( " \t\n\r" );
        last = str.find_last_not_of ( " \t\n\r" );
        if ( first == std::string::npos || last == std::string::npos )
        {
            str = std::string();
            return str;
        }

        str = str.substr ( first, last - first + 1 );
        return  str;
    }

}	/* namespace ZUtil */

#endif	/* __ZUTIL_STRING_HPP__ */
